package cn.topcodes.tcsf.wechat.web.controller;

import cn.topcode.unicorn.utils.CollectionUtil;
import cn.topcode.unicorn.wxsdk.WXContext;
import cn.topcode.unicorn.wxsdk.base.Result;
import cn.topcode.unicorn.wxsdk.user.dto.CreateUserTagResult;
import cn.topcode.unicorn.wxsdk.user.dto.GetTagListResult;
import cn.topcode.unicorn.wxsdk.user.dto.Tag;
import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.wechat.domain.entity.WechatMp;
import cn.topcodes.tcsf.wechat.domain.entity.WechatUserTag;
import cn.topcodes.tcsf.wechat.domain.form.WechatUserTagForm;
import cn.topcodes.tcsf.wechat.service.WechatMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Unicorn on 2018/2/24.
 */
@Controller
@RequestMapping("/admin/wechat/user/tag")
public class WechatUserTagController extends AdminController<WechatUserTag, WechatUserTag, WechatUserTagForm> {

    @Autowired
    private WechatMpService wechatMpService;

    @Override
    protected String getPageFolder() {
        return "wechat/admin/tags";
    }

    @Override
    public ApiResponseEntity<List<WechatUserTag>> list(HttpServletRequest request) {
        String mpId = request.getParameter("mpId");
        GetTagListResult result = WXContext.getUserInvoker().getTagList(mpId);
        if(result.getErrcode() == 0) {
            List<Tag> tags = result.getTags();
            List<Long> tagIds = CollectionUtil.getPropList(tags, "id");
            if(!tagIds.isEmpty()) {
                Example example = this.getService().buildExample();
                example.or().andNotIn(WechatUserTag.FIELD_TAG_ID, tagIds);
                this.getService().deleteByExample(example);
            }

            for(Tag tag : tags) {
                Example example = this.getService().buildExample();
                example.or().andEqualTo(WechatUserTag.FIELD_TAG_ID, tag.getId());
                WechatUserTag wechatUserTag = this.getService().findOne(example);
                if(wechatUserTag == null) {
                    wechatUserTag = new WechatUserTag();
                    wechatUserTag.setTagId(tag.getId());
                }
                WechatMp wechatMp = this.wechatMpService.findWechatMpByMpId(mpId);
                wechatUserTag.setWechatMpId(wechatMp.getId());
                wechatUserTag.setTagName(tag.getName());
                wechatUserTag.setCount(tag.getCount());
                this.getService().save(wechatUserTag);
            }
        }
        return super.list(request);
    }

    @Override
    public ApiResponseEntity create(HttpServletRequest request, WechatUserTagForm form, BindingResult result) {
        String mpId = this.wechatMpService.getMpId(form.getWechatMpId());
        CreateUserTagResult r = WXContext.getUserInvoker().createUserTag(mpId, form.getTagName());
        if(r.getErrcode() == 0) {
            WechatUserTag tag = new WechatUserTag();
            tag.setCount(0);
            tag.setWechatMpId(form.getWechatMpId());
            tag.setTagId(r.getTag().getId());
            tag.setTagName(r.getTag().getName());
            this.getService().create(tag);
            return ApiResponseEntity.buildOK();
        }else {
            return ApiResponseEntity.buildFailed(r.getErrmsg());
        }
    }

    @Override
    public ApiResponseEntity<Void> update(HttpServletRequest request, WechatUserTagForm form, BindingResult result) {
        String mpId = this.wechatMpService.getMpId(form.getWechatMpId());
        WechatUserTag tag = this.getService().findOne(form.getId());
        Result r = WXContext.getUserInvoker().editTag(mpId, tag.getTagId(), form.getTagName());
        if(r.getErrcode() == 0) {
            tag.setTagName(form.getTagName());
            this.getService().update(tag);
            return ApiResponseEntity.buildOK();
        }else {
            return ApiResponseEntity.buildFailed(r.getErrmsg());
        }
    }

    @Override
    public ApiResponseEntity carefulDelete(Long id, String password) {
        WechatUserTag tag = this.getService().findOne(id);
        String mpId = wechatMpService.getMpId(tag.getWechatMpId());
        Result result = WXContext.getUserInvoker().deleteTag(mpId, tag.getTagId());
        if(result.getErrcode() == 0) {
            return super.carefulDelete(id, password);
        }else {
            return ApiResponseEntity.buildFailed(result.getErrmsg());
        }
    }
}
